# -*- coding: iso-8859-2 -*-

import os
import logging
import traceback
import re
import string

from authorshipanalyzer.textmodifier import Editor
from authorshipanalyzer.message import Message

class EncodingFilter(Editor):
    '''
    Class removing messages send in wrong encoding.
    Usually due to not operated encoding (generally: base64)
    '''
    
    def editMessage(self, message):
        for line in message.getText().split('\n'):      
            if len(line.split()) == 1 and len(line) > 36:
                letters = {}
                for letter in line:
                    if not letters.has_key(letter):
                        letters[letter] = 0
                    letters[letter] += 1
                
                if len(letters) > 10:
                    return False
        return True
                
    def __str__(self):
        return __name__
    
if __name__ == "__main__":
     a = EncodingFilter()
     message = Message()
     message.setText("""
U2EgaSB3IFBvbHNjZSBmaXJteSwgbmlld2llbGtpZSAoa2lsa2FuYXNjaWUgb3NvYiksIGt0b3Jl
IHR5bGUgcGxhY2EgcHJhY293bmlrb20uPGJyPjxicj5CYXJ0ZWs8YnI+PGJyPjxicj48ZGl2Pjxz
cGFuIGNsYXNzPSJnbWFpbF9xdW90ZSI+T24gMTgvMDIvMjAwOCwgPGIgY2xhc3M9ImdtYWlsX3Nl
bmRlcm5hbWUiPkdyemVnb3J6IEp1bmthPC9iPiAmbHQ7PGEgaHJlZj0ibWFpbHRvOnhnanhAcG9j
enRhLm9uZXQucGwiPnhnanhAcG9jenRhLm9uZXQucGw8L2E+Jmd0OyB3cm90ZTo8L3NwYW4+PGJs
b2NrcXVvdGUgY2xhc3M9ImdtYWlsX3F1b3RlIiBzdHlsZT0iYm9yZGVyLWxlZnQ6IDFweCBzb2xp
ZCByZ2IoMjA0LCAyMDQsIDIwNCk7IG1hcmdpbjogMHB0IDBwdCAwcHQgMC44ZXg7IHBhZGRpbmct
""")
     res = a.editMessage(message)
     print (res)   
     print message.getText()
